package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理文章分类 业务模块功能
 */
@Tag(name = "文章分类管理")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 插入分类
     * ~
     * @return
     */
    @Operation(summary = "文章分类列表查询")
    @GetMapping("/admin/content/category/list")
    public Result<PageResult<ArticleCategoryVO>> pageList(ArticleCategoryQuery query){
        PageResult<ArticleCategoryVO> pageResult;
        // param为空时进行遍历查询（查询所有）
        pageResult = categoryService.pageList(query);
        return new Result<>(pageResult);
    }

    @Operation(summary = "新增分类")
    @PostMapping("/admin/content/category/insert")
    public Result<Void> add(@RequestBody ArticleCategoryParam param){
        categoryService.addCategory(param);
        return Result.success();
    }

    @Operation(summary = "编辑分类")
    @PostMapping("/admin/content/category/update")
    public Result<Void> update(@RequestBody ArticleCategoryParam param){
        if(param.getId() == null) {
            return Result.fail();
        }
        categoryService.updateCategory(param);
        return Result.success();
    }

    @Operation(summary = "删除分类")
    @PostMapping("/admin/content/category/delete")
    public Result<Void> delete(@RequestBody ArticleCategoryParam param){
        if(param.getId() == null) {
            return Result.fail();
        }
        categoryService.deleteCategory(param.getId());
        return Result.success();
    }
}
